package org.example;

import lombok.extern.slf4j.Slf4j;
import org.example.generator.DataGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * 测试产品分布逻辑
 */
@Slf4j
@SpringBootApplication
public class TestProductDistribution implements CommandLineRunner {
    
    @Autowired
    private DataGenerator dataGenerator;
    
    public static void main(String[] args) {
        SpringApplication.run(TestProductDistribution.class, args);
    }
    
    @Override
    public void run(String... args) throws Exception {
        log.info("开始测试产品分布逻辑");
        
        try {
            // 测试前6个月（只有浩瀚小贷）
            LocalDate firstMonth = LocalDate.of(2023, 7, 1);
            log.info("测试前6个月产品分布（应该只有浩瀚小贷）:");
            testProductDistribution(firstMonth, 100);
            
            // 测试第7个月开始（多产品分布）
            LocalDate seventhMonth = LocalDate.of(2024, 1, 1);
            log.info("测试第7个月开始产品分布（多产品分布）:");
            testProductDistribution(seventhMonth, 1000);
            
            log.info("产品分布测试完成！");
            
        } catch (Exception e) {
            log.error("产品分布测试过程中发生异常", e);
        }
    }
    
    private void testProductDistribution(LocalDate month, int totalOrders) {
        // 使用反射调用私有方法（仅用于测试）
        try {
            java.lang.reflect.Method method = DataGenerator.class.getDeclaredMethod("calculateProductDistribution", LocalDate.class, int.class);
            method.setAccessible(true);
            
            @SuppressWarnings("unchecked")
            Map<String, Integer> distribution = (Map<String, Integer>) method.invoke(null, month, totalOrders);
            
            log.info("月份: {}, 总订单数: {}", month, totalOrders);
            for (Map.Entry<String, Integer> entry : distribution.entrySet()) {
                String productName = entry.getKey();
                int count = entry.getValue();
                double percentage = (double) count / totalOrders * 100;
                log.info("  {}: {} 条订单 ({}%)", productName, count, String.format("%.1f", percentage));
            }
            
        } catch (Exception e) {
            log.error("调用产品分布计算方法失败", e);
        }
    }
}




